package jp.ac.tohoku.ecei.cl.www.rite;

import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.Text;
import org.w3c.dom.DOMImplementation;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.OutputKeys;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class RITEExample2RITEXML {
    
    private OutputStream os;
    private Result result;
    
    public RITEExample2RITEXML (OutputStream os) {
        this.os = os;
        this.result = new StreamResult(os);
    }

    public void pipeAll (ArrayList<RITEExample> exList) 
        throws javax.xml.transform.TransformerConfigurationException, 
               javax.xml.parsers.ParserConfigurationException,
               javax.xml.transform.TransformerException {
        this.pipeAll(exList, null);
    }

    public void pipeAll (ArrayList<RITEExample> exList, String type) 
        throws javax.xml.transform.TransformerConfigurationException, 
               javax.xml.parsers.ParserConfigurationException,
               javax.xml.transform.TransformerException {

        File xsl = new File("xml_style.xsl");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        DOMImplementation domImpl = builder.getDOMImplementation();

        Document d = domImpl.createDocument("", "dataset", null);
        Element documentEl = d.getDocumentElement();
        if (type != null) {
            Attr typeAttr = d.createAttribute("type");
            typeAttr.setValue(type);
            documentEl.setAttributeNode(typeAttr);
        }

        for (int i = 0; i < exList.size(); i++) {
            RITEExample ex = exList.get(i);
            Element pairEl = d.createElement("pair");

            Attr idAttr = d.createAttribute("id");
            idAttr.setValue(ex.getId()+"");
            pairEl.setAttributeNode(idAttr);

            Attr labelAttr = d.createAttribute("label");
            labelAttr.setValue(ex.getLabel());
            pairEl.setAttributeNode(labelAttr);

            appendExElements(ex, d, pairEl);
            documentEl.appendChild(pairEl);
        }

        // output XML -------
        TransformerFactory transfactory = TransformerFactory.newInstance();
        Source xslSource = new StreamSource(xsl);
        Transformer transformer = transfactory.newTransformer(xslSource);

        DOMSource source = new DOMSource(d);
        transformer.transform(source, result);
    }

    private void appendExElements (RITEExample ex, Document d, Element pairEl) {
        Element t1El = d.createElement("t1");
        Text t1Text = d.createTextNode(ex.getT1());
        t1El.appendChild(t1Text);
        pairEl.appendChild(t1El);

        Element t2El = d.createElement("t2");
        Text t2Text = d.createTextNode(ex.getT2());
        t2El.appendChild(t2Text);
        pairEl.appendChild(t2El);
    }
}
